package cn.huobi.product.controller;

import cn.huobi.basic.util.AjaxResult;
import cn.huobi.basic.util.LoginContext;
import cn.huobi.basic.util.PageList;
import cn.huobi.org.domain.Employee;
import cn.huobi.org.service.EmployeeService;
import cn.huobi.pet.dto.AcceptOrderDto;
import cn.huobi.product.domain.ProductOrderDetail;
import cn.huobi.product.query.ProductOrderDetailQuery;
import cn.huobi.product.service.IProductOrderDetailService;
import cn.huobi.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/productOrderDetail")
public class ProductOrderDetailController {
    @Autowired
    public IProductOrderDetailService productOrderDetailService;
    @Autowired
    private EmployeeService employeeService;


    /**
     * 保存和修改公用的
     *
     * @param productOrderDetail 传递的实体
     * @return AjaxResult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody ProductOrderDetail productOrderDetail) {
        try {
            if (productOrderDetail.getId() != null)
                productOrderDetailService.update(productOrderDetail);
            else
                productOrderDetailService.add(productOrderDetail);
            return AjaxResult.me().success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().fail();
        }
    }

    /**
     * 删除对象信息
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            productOrderDetailService.delete(id);
            return AjaxResult.me().success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().fail();
        }
    }

    //获取用户
    @GetMapping("/{id}")
    public ProductOrderDetail get(@PathVariable("id") Long id) {
        return productOrderDetailService.findById(id);
    }


    /**
     * 查看所有的员工信息
     *
     * @return
     */
    @GetMapping()
    public List<ProductOrderDetail> list() {
        return productOrderDetailService.findAll();
    }


    /**
     * 分页查询数据
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping("/list")
    public PageList<ProductOrderDetail> json(@RequestBody ProductOrderDetailQuery query, HttpServletRequest request) {
        Logininfo logininfo = LoginContext.getLoginContext(request);
        Employee employee = employeeService.findByLogininfoId(logininfo.getId());
        query.setState(0);
        query.setShopId(employee.getShopId());
        return productOrderDetailService.queryByPage(query);
    }


    /**
     * 接收订单
     */
    @PostMapping("/accept")
    public AjaxResult accept(@RequestBody AcceptOrderDto acceptOrderDto) {
        productOrderDetailService.accept(acceptOrderDto);
        return AjaxResult.me().success();
    }


    /**
     * 拒绝定单
     */
    @PostMapping("/reject/{msgId}")
    public AjaxResult reject(@PathVariable("msgId") Long msgId,HttpServletRequest request) {
        Logininfo logininfo = LoginContext.getLoginContext(request);
        Employee employee = employeeService.findByLogininfoId(logininfo.getId());
        productOrderDetailService.reject(msgId,employee);
        return AjaxResult.me().success();
    }

}
